<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mtranslate extends CI_Model {

    public $_table = 'tpt_translate';

    function __construct() {
        parent::__construct();
		$this->load->database();
    }

    public function find($name) {
        $this->db->where('word', $name);
        $query = $this->db->get($this->table);
        $input = $query->result_array();
        $word = '';
        foreach ($input as $key => $item) {
            if ($word != $item['word']) {
                if ($word != '') {
                    $list[] = $temp;
                }
                $temp['word'] = $item['word'];
                $word = $item['word'];
            }
            $temp['translate'][$item['lang_code']] = $item['translate'];
        }
        $list = $temp;
        return $list;
    }

    public function insert($data) {
		$this->db->select_max('tran_id');
		$query = $this->db->get($this->_table);
		$row = $query->row_array();
		$id_next = $row['tran_id'] + 1;
		$dataLang = $this->registry->get('language');
		foreach($dataLang as $lang){
			$data_insert = array(
				'tran_id' => $id_next,
				'word' => $data['keyword'],
				'lang_code' => $lang['lang_code'],
				'translate' => $data['translate_'.$lang['lang_code']]
			);
	        $this->db->insert($this->_table, $data_insert);
		}
        $this->export_json();
    }

    public function check_word($word, $code, $self = '') {
        $this->db->select('COUNT(tran_id) as c');
        if ($self != '') {
            $this->db->where('word !=', $self);
        }
        $this->db->where('word', $word);
        $this->db->where('lang_code', $code);
        $query = $this->db->get($this->_table);
        $count = $query->result_array();
        $count = $count[0]['c'];
        if ($count > 0) {
            return false;
        } else {
            return true;
        }
    }

    public function update($data, $id) {
        $this->db->where('word', $data['keyword']);
		$dataLang = $this->registry->get('language');
        foreach($dataLang as $lang){
			$this->db->where('lang_code', $lang['lang_code']);
			$query = $this->db->get($this->_table);
			if ($query->num_rows() > 0) {
				$data_update = array(
					'translate' => $data['translate_'.$lang['lang_code']]
				);
	            $this->db->where('tran_id', $id);
	            $this->db->where('lang_code', $lang['lang_code']);
	            $this->db->update($this->_table, $data_update);
	        } else {
				$data_insert = array(
					'tran_id' => $id,
					'word' => $data['keyword'],
					'lang_code' => $lang['lang_code'],
					'translate' => $data['translate_'.$lang['lang_code']]
				);
	            $this->db->insert($this->_table, $data_insert);
	        }
		}
        $this->export_json();
    }

    public function delete($id) {
        $this->db->where('tran_id', $id);
        $this->db->delete($this->_table);
        $this->export_json();
    }

    public function get($word) {
        // $lang_code = $this->session->userdata('current_language');
        // $lang_code_default = $this->session->userdata('default_language');
        // $lang_code_default = $lang_code_default['code'];
        // $lang_code = $lang_code['code'];
        // $this->db->where('word', $word);
        // $query = $this->db->get($this->_table);
        // $translate = $query->result();
        // $t = array();
        // if ($translate) {
        //     foreach ($translate as $key => $value) {
        //         $t[$value->lang_code] = $value->translate;
        //     }
        //     if (isset($t[$lang_code]) == TRUE && $t[$lang_code] != '') {
        //         return $t[$lang_code];
        //     } elseif (isset($t[$lang_code_default]) == TRUE && $t[$lang_code_default] != '') {
        //         return $t[$lang_code_default];
        //     } else {
        //         return $word;
        //     }
        // }
        // return $word;
        $lang_code = $this->session->userdata('current_language');
        $lang_code_default = $this->session->userdata('default_language');
        $lang_code_default = $lang_code_default['code'];
        $lang_code = $lang_code['code'];
        $fileSetting = json_decode(file_get_contents(APPPATH.'../assets/json/translate.json'), true);

        if (isset($fileSetting[$word][$lang_code]) == TRUE && $fileSetting[$word][$lang_code] != '') {
            return $fileSetting[$word][$lang_code];
        } elseif (isset($fileSetting[$word][$lang_code_default]) == TRUE && $fileSetting[$word][$lang_code_default] != '') {
            return $fileSetting[$word][$lang_code_default];
        } else {
            return $word;
        }
        return $word;
    }

	public function get_data($id){
		$this->db->where('tran_id',$id);
		$result = $this->db->get($this->_table)->result_array();
		foreach($result as $rs){
			$data['tran_id'] = $rs['tran_id'];
			$data['keyword'] = $rs['word'];
			if($rs['lang_code'] == 'en'){
				$data['translate_en'] = $rs['translate'];
			}else{
				$data['translate_vn'] = $rs['translate'];
			}
		}
		return $data;
	}

    public function export_json(){
        $rows = $this->db->get($this->_table)->result_array();
        $dataTranslate = array();
        foreach($rows as $value){
            unset($value['id']);
            $dataTranslate[$value['word']][$value['lang_code']] = $value['translate'];                 
        }
        $fp = fopen(APPPATH.'../assets/json/translate.json', 'w');
        fwrite($fp, json_encode($dataTranslate));
        fclose($fp);
    }
}
